package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.model.Shopcart;
import com.model.User;
import com.util.JdbcUtil;
import com.model.Orders;
import com.model.Product;

public class ShopcartDao {
	
	JdbcUtil jdbc=new JdbcUtil();
	//查询某位用户的出售页	物品数量
	public Integer getCartCountByUid(Integer uid){
		String sql ="select count(sid) record from shopcart where uid="+uid;
		int rowCount = 0;
		try {
			System.out.println(sql);
			ResultSet rs = jdbc.queryStatement(sql);
			if(rs.next()){
				rowCount=rs.getInt("record");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		jdbc.close();
		return rowCount;
	}
	//出售页进行分页展示
	public List<Shopcart> queryCart_show(Shopcart cart,int cp,int pagesize){
		List<Shopcart> carts = null;
		StringBuffer sql = new StringBuffer("select * from shopcart where 1=1");
		if(cart.getSid()!=null&&cart.getSid()!=0){
			sql.append(" and sid = "+cart.getSid());
		}
		if(cart.getUid()!=null&&cart.getUid()!=0){
			sql.append(" and uid = "+cart.getUid());
		}
		if(cart.getPid()!=null&&cart.getPid()!=0){
			sql.append(" and pid  =  "+cart.getPid());
		}
		sql.append(" order by sid  desc");
		//外层分页语句
		StringBuffer sb = new StringBuffer();
		sb.append(sql);
		sb.append(" limit "+(cp-1)*pagesize+","+(cp)*pagesize);
		System.out.println(sb.toString());
		carts = jdbc.queryPreparedStatement(sb.toString(), Shopcart.class);
		jdbc.close();
		return carts;
	}
	//通过uid,pid查询某位用户的出售页的一条记录--验证出售页是否已经加入该件商品
	public  List<Shopcart> queryCart(Shopcart cart){
		List<Shopcart> carts = null;
		StringBuffer sb = new StringBuffer("select * from shopcart where 1=1");
		if(cart.getSid()!=null&&cart.getSid()!=0){
			sb.append(" and sid = "+cart.getSid());
		}
		if(cart.getUid()!=null&&cart.getUid()!=0){
			sb.append(" and uid = "+cart.getUid());
		}
		if(cart.getPid()!=null&&cart.getPid()!=0){
			sb.append(" and pid  =  "+cart.getPid());
		}
		System.out.println(sb.toString());
		carts = jdbc.queryPreparedStatement(sb.toString(), Shopcart.class);
		jdbc.close();
		return carts;
	}
	//通过uid,pid来更新某位用户从产品展示操作的出售页的一条记录
	public void updateCartByUidPid(int uid,int pid,double weight) throws Exception{
		String sql="update shopcart set weight=weight+? where uid=? and pid=?";
		jdbc.updatePreparedStatement(sql,weight,uid,pid);
		jdbc.close();
	}
	//添加一条出售页信息
	public void add(Shopcart cart) throws Exception{
		String sql="insert into shopcart values(?,?,?,?,?,?,?)";
		jdbc.updatePreparedStatement(sql, null,cart.getUid(),cart.getPid(),
				cart.getPname(),cart.getPimage(),cart.getPprice(),cart.getWeight());
		jdbc.close();
	}
	//通过sid删除一条出售页信息
	public void deleteBySid(int sid) throws Exception{
		String sql ="delete from shopcart where sid=? ";
		jdbc.updatePreparedStatement(sql,sid);
		jdbc.close();
	}
	//通过uid删除某位用户的所有出售页信息
	public void deleteByUid(int uid) throws Exception{
		String sql ="delete from shopcart where uid=? ";
		jdbc.updatePreparedStatement(sql,uid);
		jdbc.close();
	}
	//出售页点击加减按钮更新数据库的一条记录
	public void updateCartWeight(int sid,String yunsuan) throws Exception{
		StringBuffer sb = new StringBuffer("update  shopcart set weight=weight");
		if(yunsuan.equals("+")){
			sb.append(" +1 ");
		}else if(yunsuan.equals("-")){
			sb.append(" -1 ");
		}
		sb.append(" where sid=?");
		jdbc.updatePreparedStatement(sb.toString(),sid);
		jdbc.close();
	}
}